/************************************************************
 * 文件名: selfinfowidget.cpp
 * 功能描述: 实现个人信息窗口的具体界面与编辑逻辑。
 * 作者: 李强
 * 创建日期: [请填写日期]
 * 修改历史: 
 ************************************************************/
#include "selfinfowidget.h"

#include <QCursor>
#include <QGridLayout>



SelfInfoWidget::SelfInfoWidget(QWidget* parent) : QDialog(parent) {
    //1. 设置整个窗口的属性
    this->setFixedSize(500,250);
    this->setWindowTitle("个人信息");
    this->setWindowIcon(QIcon(":/recource/image/yang.jpg"));
    // 窗口被关闭时，自动销毁这个对话框对象
    this->setAttribute(Qt::WA_DeleteOnClose);
    //把窗口移动到鼠标当前的位置
    this->move(QCursor::pos());


    //2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    //layout->setSpacing(0);
    layout->setHorizontalSpacing(10);
    layout->setVerticalSpacing(3);
    layout->setContentsMargins(20,20,20,0);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    //3. 创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75,75);
    avatarBtn->setIconSize(QSize(75,75));
    avatarBtn->setIcon(QIcon(":/recource/image/yang.jpg"));
    avatarBtn->setStyleSheet("QPushButton { border: none; background-color:transparent;}");
    layout->addWidget(avatarBtn,0,0,3,1);


    QString labelStyle = "QLabel { font-size: 14px; font-weight: 800; }";
    QString btnStyle = "QPushButton { border: none; background-color: transparent; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }";
    QString editStyle = "QLineEdit { border : none ; border-radius:5px; padding-left:2px;}";

    int hight = 30;

    //4. 创建用户的 id 显示
    idTag = new QLabel();
    idTag->setFixedSize(50,hight);
    idTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    idTag->setText("序号");
    idTag->setStyleSheet(labelStyle);

    idLabel = new QLabel();
    idLabel->setFixedHeight(hight);
    idLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    //5.添加用户的名字显示
    nameTag = new QLabel();
    nameTag->setFixedSize(50,hight);
    nameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    nameTag->setText("昵称");
    nameTag->setStyleSheet(labelStyle);

    nameLabel = new QLabel();
    nameLabel->setFixedHeight(hight);
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    nameModifyBtn = new QPushButton();
    nameModifyBtn->setFixedSize(70,25);
    nameModifyBtn->setIconSize(QSize(20,20));
    nameModifyBtn->setIcon(QIcon(":/recource/image/modify.png"));
    nameModifyBtn->setStyleSheet(btnStyle);


    nameEdit = new QLineEdit();
    nameEdit->setFixedHeight(hight);
    nameEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameEdit->setStyleSheet(editStyle);
    nameEdit->hide();

    nameSubmitBtn = new QPushButton();
    nameSubmitBtn->setFixedSize(70,25);
    nameSubmitBtn->setIconSize(QSize(20,20));
    nameSubmitBtn->setIcon(QIcon(":/recource/image/submit.png"));
    nameSubmitBtn->setStyleSheet(btnStyle);
    nameSubmitBtn->hide();

    //6. 添加个性签名
    descTag = new QLabel();
    descTag->setFixedSize(50,hight);
    descTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    descTag->setText("签名");
    descTag->setStyleSheet(labelStyle);

    descLabel = new QLabel();
    descLabel->setFixedHeight(hight);
    descLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);


    descModifyBtn = new QPushButton();
    descModifyBtn->setFixedSize(70,25);
    descModifyBtn->setIconSize(QSize(20,20));
    descModifyBtn->setIcon(QIcon(":/recource/image/modify.png"));
    descModifyBtn->setStyleSheet(btnStyle);

    descEdit = new QLineEdit();
    descEdit->setFixedHeight(hight);
    descEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descEdit->setStyleSheet(editStyle);
    descEdit->hide();

    descSumbitBtn = new QPushButton();
    descSumbitBtn->setFixedSize(70,25);
    descSumbitBtn->setIconSize(QSize(20,20));
    descSumbitBtn->setIcon(QIcon(":/recource/image/submit.png"));
    descSumbitBtn->setStyleSheet(btnStyle);
    descSumbitBtn->hide();

    //7.添加电话
    phoneTag = new QLabel();
    phoneTag->setFixedSize(50, hight);
    phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(labelStyle);

    phoneLabel = new QLabel();
    phoneLabel->setFixedHeight(hight);
    phoneLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    phoneModifyBtn = new QPushButton();
    phoneModifyBtn->setFixedSize(70, 25);
    phoneModifyBtn->setIconSize(QSize(20, 20));
    phoneModifyBtn->setIcon(QIcon(":/recource/image/modify.png"));
    phoneModifyBtn->setStyleSheet(btnStyle);

    phoneEdit = new QLineEdit();
    phoneEdit->setFixedHeight(hight);
    phoneEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    phoneEdit->setStyleSheet(editStyle);
    phoneEdit->hide();

    phoneSubmitBtn = new QPushButton();
    phoneSubmitBtn->setFixedSize(70, 25);
    phoneSubmitBtn->setIconSize(QSize(20, 20));
    phoneSubmitBtn->setIcon(QIcon(":/recource/image/submit.png"));
    phoneSubmitBtn->setStyleSheet(btnStyle);
    phoneSubmitBtn->hide();

    //8. 添加验证码
    verifyCodeTag = new QLabel();
    verifyCodeTag->setFixedSize(50,hight);
    verifyCodeTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    verifyCodeTag->setText("验证码");
    verifyCodeTag->setStyleSheet(labelStyle);
    verifyCodeTag->hide();

    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setFixedHeight(hight);
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    verifyCodeEdit->setStyleSheet(editStyle);
    verifyCodeEdit->hide();


    getverifyCodeBtn = new QPushButton();
    getverifyCodeBtn->setText("获取验证码");
    getverifyCodeBtn->setStyleSheet("QPushButton {border:none; background-color:transparent;} QPushButton:pressed {background-color:rgb(231，231,231）;");
    getverifyCodeBtn->setFixedSize(70,hight);
    getverifyCodeBtn->hide();


    //9. 添加到布局管理器
    layout->addWidget(idTag,0,1);
    layout->addWidget(idLabel,0,2);

    layout->addWidget(nameTag,1,1);
    layout->addWidget(nameLabel,1,2);
    layout->addWidget(nameModifyBtn,1,3);

    layout->addWidget(descTag,2,1);
    layout->addWidget(descLabel,2,2);
    layout->addWidget(descModifyBtn,2,3);

    layout->addWidget(phoneTag,3,1);
    layout->addWidget(phoneLabel,3,2);
    layout->addWidget(phoneModifyBtn,3,3);

    // layout->addWidget(verifyCodeTag,4,1);
    // layout->addWidget(verifyCodeEdit,4,2);
    // layout->addWidget(getverifyCodeBtn,4,3);

#if TEST_UI
    idLabel->setText("1234");
    nameLabel->setText("张三");
    phoneLabel->setText("12345678");
     descLabel->setText("我要好好学编程");
#endif

     //10. 连接信号槽
     connect(nameModifyBtn, &QPushButton::clicked,this,[=](){
         // 把当前的 nameLabel 和 nameModifyBtn 隐藏起来
         nameLabel->hide();
         nameModifyBtn->hide();
         layout->removeWidget(nameLabel);
         layout->removeWidget(nameModifyBtn);
         // 把 nameEdit 和 nameSubmitBtn 显示出来
         nameEdit->show();
         nameSubmitBtn->show();
         layout->addWidget(nameEdit,1,2);
         layout->addWidget(nameSubmitBtn,1,3);
         //把输入框的内容进行设置
         nameEdit->setText(nameLabel->text());
     });


     connect(descModifyBtn, &QPushButton::clicked,this,[=](){
         descLabel->hide();
         descModifyBtn->hide();
         layout->removeWidget(descLabel);
         layout->removeWidget(descModifyBtn);

         descEdit->show();
         descSumbitBtn->show();
         layout->addWidget(descEdit,2,2);
         layout->addWidget(descSumbitBtn,2,3);

         descEdit->setText(descLabel->text());
     });

     connect(phoneModifyBtn, &QPushButton::clicked,this,[=](){
         phoneLabel->hide();
         phoneModifyBtn->hide();
         layout->removeWidget(phoneLabel);
         layout->removeWidget(phoneModifyBtn);

         phoneEdit->show();
         phoneSubmitBtn->show();
         layout->addWidget(phoneEdit,3,2);
         layout->addWidget(phoneSubmitBtn,3,3);

         verifyCodeTag->show();
         verifyCodeEdit->show();
         getverifyCodeBtn->show();
         layout->addWidget(verifyCodeTag,4,1);
         layout->addWidget(verifyCodeEdit,4,2);
         layout->addWidget(getverifyCodeBtn,4,3);


         phoneEdit->setText(phoneLabel->text());
     });

}
